package com.dwxt.cdms.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 血压实体类
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
@Table(name = "t_blutdruck")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Blutdruck {
    @Id
    private String id;

    /**
     * 起始碼
     */
    private String start;

    /**
     * 版本号
     */
    private String version;

    /**
     * 用户号
     */
    private String user;

    /**
     * 客戶碼
     */
    private String customer;

    /**
     * 机型代码
     */
    @Column(name = "model_code")
    private String modelCode;

    /**
     * 机种码
     */
    @Column(name = "device_type")
    private String deviceType;

    /**
     * 系列号
     */
    @Column(name = "serial_number")
    private String serialNumber;

    /**
     * 高压
     */
    @Column(name = "high_data")
    private String highData;

    /**
     * 低压
     */
    @Column(name = "low_data")
    private String lowData;

    /**
     * 心率
     */
    @Column(name = "heart_data")
    private String heartData;

    /**
     * 年
     */
    private String year;

    /**
     * 月
     */
    private String month;

    /**
     * 日
     */
    private String day;

    /**
     * 时
     */
    private String hour;

    /**
     * 分
     */
    private String minute;

    /**
     * 检查时间
     */
    @Column(name = "check_time")
    private String checkTime;

    /**
     * 校驗碼
     */
    @Column(name = "check_code")
    private String checkCode;

    /**
     * 连接符
     */
    private String separator43;

    /**
     * IMSI
     */
    private String imsi;

    /**
     * 连接符
     */
    private String separator59;

    /**
     * IMEI和保留字符
     */
    @Column(name = "latiLongitude")
    private String latiLongitude;

    /**
     * 请求的原始数据
     */
    @Column(name = "src_data")
    private String srcData;

    /**
     * 客户id
     */
    @Column(name = "customer_id")
    private Integer customerId;

    /**
     * 爱奥乐推送记录id
     */
    @Column(name = "aiaole_id")
    private String aiaoleId;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    /**
     * 逻辑删除标识(0否1是)
     */
    @Column(name = "is_delete")
    private String isDelete;

    /**
     * 备用字段1
     */
    private String arrt1;

    /**
     * 角色id
     */
    @Column(name = "part_id")
    private Integer partId;



    @Transient
    private String crcCode;

    /**
     * 数据类型
     */
    @Transient
    private String dataType;

    public Blutdruck(String srcdata) {
        this.srcData = srcdata;
        parseData();
    }

    protected void parseData() {
        this.start = this.srcData.substring(0, 2);
        this.version = this.srcData.substring(2, 4);
        this.user = this.srcData.substring(4, 5);
        this.customer = this.srcData.substring(5, 7);
        this.modelCode = this.srcData.substring(7, 9);
        this.deviceType = this.srcData.substring(9, 11); // 机种码
        this.serialNumber = this.srcData.substring(11, 20);

        this.highData = String.valueOf(Integer.parseInt(this.srcData.substring(20, 23)));
        this.lowData = String.valueOf(Integer.parseInt(this.srcData.substring(23, 26)));
        // 血糖仪是以mg/dL为单位传送，如果要显示为mmol/dL的单位，则要除以18，并四舍五入才能与血糖仪端显示的数据一致
        this.heartData = this.srcData.substring(26, 29);
        this.year = this.srcData.substring(29, 31);
        this.month = this.srcData.substring(31, 33);
        this.day = this.srcData.substring(33, 35);
        this.hour = this.srcData.substring(35, 37);
        this.minute = this.srcData.substring(37, 39);
        this.checkCode = this.srcData.substring(39, 43);
        this.separator43 = this.srcData.substring(43, 44);
        this.imsi = this.srcData.substring(44, 59);
        this.separator59 = this.srcData.substring(59, 60);
        this.latiLongitude = this.srcData.substring(60, 85);

        /** 检查时间*/
        int year = Integer.valueOf("20" + this.year);
        int month = Integer.valueOf(this.month);
        int day = Integer.valueOf(this.day);
        int hour = Integer.valueOf(this.hour);
        int minute = Integer.valueOf(this.minute);
//        System.err.println(year+"_"+month+"_"+day+"_"+hour+"_"+minute);
        LocalDateTime localDateTime = LocalDateTime.of(
                year, month, day, hour, minute);
//        System.err.println(JSON.toJSON(localDateTime));
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = localDateTime.format(fmt);

        this.checkTime = dateStr;

        if (this.srcData.length() > 85) {
            this.crcCode = this.srcData.substring(85);
        }
    }


}